package com.czl.module_storehouse.activity.receive.change;

import android.os.Bundle;
import android.view.LayoutInflater;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.ReceiveChangeModelAdapter;
import com.czl.module_storehouse.databinding.ActivityReceiveChangeModelBinding;
import com.czl.module_storehouse.event.ReceiveChangeModelEvent;
import com.czl.module_storehouse.event.ReceiveChangeModelPostEvent;
import com.czl.module_storehouse.mvp.presenter.SortPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


/**
 * @author xubin
 */
public class ReceiveChangeModelActivity extends BaseActivity<ActivityReceiveChangeModelBinding> implements SimpleView {

    private ReceiveChangeModelAdapter mAdapter;
    private List<SortBean> mExistSortBeans;

    @InjectPresenter
    SortPresenter mSortPresenter;

    @Override
    protected ActivityReceiveChangeModelBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityReceiveChangeModelBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("选择物品型号");
        mAdapter = new ReceiveChangeModelAdapter(R.layout.item_receive_change_model, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        EventBus.getDefault().register(this);
        mAdapter.setOnItemClickListener((adapter, view, position) -> popActivity(position));
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(ReceiveChangeModelEvent event) {
        SortBean sortBean = event.getSortBean();
        if (sortBean == null) {
            return;
        }
        mExistSortBeans = new ArrayList<>();
        mExistSortBeans.add(sortBean);
        binding.tvName.setText(sortBean.getSortName());
        if (event.getSortBeans() != null) {
            mExistSortBeans.addAll(event.getSortBeans());
        }
    }

    @Override
    protected void loadData() {
        super.loadData();
        String sortName = binding.tvName.getText().toString();
        if (mSortPresenter != null) {
            mSortPresenter.getSortList(sortName);
        }
    }

    private void popActivity(int position) {
        SortBean item = mAdapter.getItem(position);
        if (item == null) {
            return;
        }
        if (item.stockNum() > 0) {
            EventBus.getDefault().postSticky(new ReceiveChangeModelPostEvent(item));
            finish();
        } else {
            showToast("该物品库存不足");
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        ListBean<SortBean> listBean = (ListBean<SortBean>) t;
        List<SortBean> list = listBean.getList();
        if (list == null || list.isEmpty()) {
            showUnKnowError("没有可更换的物品", R.mipmap.ic_empty);
            return;
        }
        List<SortBean> newList = new ArrayList<>(list);
        for (SortBean sortBean : list) {
            if (mExistSortBeans != null) {
                for (SortBean bean : mExistSortBeans) {
                    if (sortBean.getSortId() == null) {
                        continue;
                    }
                    if (sortBean.getSortId().equals(bean.getSortId())) {
                        newList.remove(sortBean);
                        break;
                    }
                }
            }
        }
        if (newList.isEmpty()) {
            showUnKnowError("没有可更换的物品", R.mipmap.ic_empty);
        } else {
            mAdapter.addData(newList);
        }
    }
}
